package net.rong.mobileim;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import net.x52im.mobileimsdk.android.event.MessageQoSEvent;
import net.x52im.mobileimsdk.server.protocal.Protocal;

import java.util.ArrayList;

public class MessageQoSEventListener implements MessageQoSEvent {

    private final ReactApplicationContext reactContext;

    public MessageQoSEventListener(ReactApplicationContext reactContext) {
        this.reactContext = reactContext;
    }

    @Override
    public void messagesLost(ArrayList<Protocal> lostMessages) {
        WritableMap params = Arguments.createMap();
        params.putInt("count", lostMessages.size());

        reactContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(EventConst.MESSAGES_LOST, params);
    }

    @Override
    public void messagesBeReceived(String fp) {
        WritableMap params = Arguments.createMap();
        params.putString("fp", fp);

        reactContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(EventConst.MESSAGES_BE_RECEIVED, params);
    }

}
